package com.zb.set.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zb.entity.AccounLoginInfo;
import com.zb.entity.ZjCustomer;
import com.zb.entity.ZjNetworkInviteCodeSet;
import com.zb.exception.SMSException;
import com.zb.exception.ZbException;
import com.zb.service.SmsVerificCodeService;
import com.zb.service.ZjCustomerService;
import com.zb.service.ZjNetworkInviteCodeSetService;
import com.zb.set.service.ConSetService;
import com.zb.sms.service.SmsSendMessageService;
import com.zb.utils.VerificUtil;
import com.zb.vo.NetworkInviteCodeSetVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @Author WEN
 * @Date 2024/4/3 0003
 **/
@Slf4j
@Service
public class ConSetServiceImpl implements ConSetService {
    @Resource
    private ZjNetworkInviteCodeSetService networkInviteCodeSetService;
    @Resource
    private ZjCustomerService customerService;

    // 重新生成邀请码
    @Override
    public Object generateInviteCode(AccounLoginInfo loginInfo) {
        NetworkInviteCodeSetVo codeSetVo = networkInviteCodeSetService.getNetworkInviteCodeSet(loginInfo.getCustAccId());
        // 删除旧的邀请码
        if (StringUtils.isNotBlank(codeSetVo.getInviteCode())) {
            SmsVerificCodeService.deleteInviteCode(codeSetVo.getInviteCode());
        }
        // 生成新的邀请码并缓存
        log.info("生成新的邀请码：{}", loginInfo);
        String inviteCode = SmsVerificCodeService.generateInviteCode(loginInfo.getCustAccId(), loginInfo.getNetworkId(), codeSetVo.getExpirtTime() * 24 * 60 * 60L);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("inviteCode", inviteCode);

        // 更新邀请码
        ZjNetworkInviteCodeSet codeSet = new ZjNetworkInviteCodeSet();
        codeSet.setId(loginInfo.getCustAccId());
        codeSet.setInviteCode(inviteCode);
        networkInviteCodeSetService.updateById(codeSet);
        return resultMap;
    }

    @Override
    public void sendSmsInviteCode(AccounLoginInfo loginInfo, List<String> phones) throws SMSException, ZbException {
        Assert.notEmpty(phones, "接收者的手机号不能为空");
        List<String> phoneRegs = phones.stream().filter(f -> !Pattern.matches(VerificUtil.REGEX_PHONE, f)).toList();
        Assert.isTrue(CollectionUtil.isEmpty(phoneRegs), "接收者手机号格式错误：" + String.join("、", phoneRegs));

        ZjNetworkInviteCodeSet codeSet = networkInviteCodeSetService.getById(loginInfo.getCustAccId());
        Assert.isTrue(StringUtils.isNotBlank(codeSet.getInviteCode()), "邀请码为空，无法发送邀请");
        // 校验邀请码
        SmsVerificCodeService.checkInviteCode(codeSet.getInviteCode());

        ZjCustomer customer = customerService.getById(loginInfo.getCustAccId());
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("enterprise", customer.getFullName());
        paramMap.put("invite_code", codeSet.getInviteCode());
        paramMap.put("param", "RT"+ codeSet.getInviteCode());
        SmsSendMessageService.sendInviteCode(loginInfo, phones, paramMap);
    }
}
